<?php
/*
Plugin Name: 云片网络
Description: 使用云片网络网络发送手机短信
Plugin URI: http://blog.wpjam.com/
Author URI: http://blog.wpjam.com/
Version: 0.1
*/

function wpjam_yunpian_send_sms($mobile, $code='', $tpl_content=''){
	$tpl_content	= ($tpl_content)?$tpl_content:wpjam_yunpian_get_default_tpl_content();
	if(is_wp_error($tpl_content)){
		return $tpl_content;
	}
	// #content#
	$text	= str_replace('#code#', $code, $tpl_content);
	$url	= 'https://sms.yunpian.com/v1/sms/send.json';
	return  wpjam_yunpian_remote_request($url, compact('mobile','text'));
}

function wpjam_yunpian_get_sms($data=array()){
	$url	= 'https://sms.yunpian.com/v1/sms/get.json';

	$data 	= wp_parse_args($data, array(
		'start_time'	=> gmdate( 'Y-m-d H:i:s', (current_time('timestamp')-DAY_IN_SECONDS*30)),
		'end_time'		=> current_time('mysql'),
		'page_num'		=> 1,
		'page_size'		=> 100
	));

	$response = wpjam_yunpian_remote_request($url, $data);

	if(is_wp_error($response)){
		return $response;
	}

	return $response['sms']; 
}

function wpjam_yunpian_get_sms_count($data=array()){
	$url	= 'https://sms.yunpian.com/v1/sms/count.json';

	$data 	= wp_parse_args($data, array(
		'start_time'	=> gmdate( 'Y-m-d H:i:s', (current_time('timestamp')-DAY_IN_SECONDS*30)),
		'end_time'		=> current_time('mysql'),
		'page_num'		=> 1,
		'page_size'		=> 100
	));

	$response = wpjam_yunpian_remote_request($url, $data);

	if(is_wp_error($response)){
		return $response;
	}

	return $response['total']; 
}

function wpjam_yunpian_get_tpl($tpl_id=''){
	$url	= 'https://sms.yunpian.com/v1/tpl/get.json';
	$response = wpjam_yunpian_remote_request($url, compact('tpl_id'));

	if(is_wp_error($response)){
		return $response;
	}

	return $response['template'];
}

function wpjam_yunpian_get_all_tpls(){
	$all_tpls = get_transient('wpjam_yunpian_all_tpls');
	if($all_tpls === false){
		$all_tpls = wpjam_yunpian_get_tpl();
		
		if(is_wp_error($all_tpls)){
			return $all_tpls;
		}

		set_transient('wpjam_yunpian_all_tpls', $all_tpls, DAY_IN_SECONDS*10);
	}
	return $all_tpls;
}


function wpjam_yunpian_get_default_tpl($tpl_id=1){
	$url	= 'http://yunpian.com/v1/tpl/get_default.json';
	return  wpjam_yunpian_remote_request($url, compact('tpl_id'));
}

function wpjam_yunpian_add_tpl($tpl_content, $notify_type=0){
	delete_transient('wpjam_yunpian_all_tpls');

	$url = 'https://sms.yunpian.com/v1/tpl/add.json';
	return  wpjam_yunpian_remote_request($url, compact('tpl_content','notify_type'));
}

function wpjam_yunpian_update_tpl($tpl_id, $tpl_content){
	delete_transient('wpjam_yunpian_all_tpls');

	$url = 'https://sms.yunpian.com/v1/tpl/update.json';
	return  wpjam_yunpian_remote_request($url, compact('tpl_id', 'tpl_content'));
}

function wpjam_yunpian_del_tpl($tpl_id){
	delete_transient('wpjam_yunpian_all_tpls');

	$url = 'https://sms.yunpian.com/v1/tpl/del.json';
	return  wpjam_yunpian_remote_request($url, compact('tpl_id'));
}

function wpjam_yunpian_get_default_tpl_content(){
	$tpl_content	= get_transient('wpjam_yunpian_tpl_content');

	if($tpl_content === false){
		if($tpl_id	= wpjam_get_setting('wpjam-yunpian', 'tpl_id')) {
			$response = wpjam_yunpian_get_tpl($tpl_id);
			
			if(is_wp_error($response)){
				return $response;
			}

			$tpl_content = $response['tpl_content'];

			set_transient('wpjam_yunpian_tpl_content', $tpl_content, DAY_IN_SECONDS*10 );
		}
	}
	
	return $tpl_content;
}

function wpjam_yunpian_get_user(){
	$user	= get_transient('wpjam_yunpian_user');
	if($user === false){
		$response = wpjam_yunpian_remote_request('https://sms.yunpian.com/v1/user/get.json');

		if(is_wp_error($response)){
			return $response;
		}

		$user = $response['user'];
		set_transient('wpjam_yunpian_user', $user, DAY_IN_SECONDS);
	}

	return $user;
}

function wpjam_yunpian_set_user($data){
	delete_transient('wpjam_yunpian_user');
	
	$response = wpjam_yunpian_remote_request('https://sms.yunpian.com/v1/user/set.json', $data);

	if(is_wp_error($response)){
		return $response;
	}

	return true;
}

function wpjam_yunpian_send_voice($mobile, $code){
	$url = 'https://voice.yunpian.com/v1/voice/send.json';
	return  wpjam_yunpian_remote_request($url, compact('mobile','code'));
}

function wpjam_yunpian_get_flow_package(){
	$flow_package	= get_transient('wpjam_yunpian_flow_package');
	if($flow_package === false){
		$response = wpjam_yunpian_remote_request('https://flow.yunpian.com/v1/flow/get_package.json');
		if(is_wp_error($response)){
			return $response;
		}
		$flow_package = $response['flow_package'];
		set_transient('wpjam_yunpian_flow_package', $flow_package, DAY_IN_SECONDS);
	}

	return $flow_package;
}

function wpjam_yunpian_recharge_flow($mobile, $sn){
	$url = 'https://flow.yunpian.com/v1/flow/recharge.json';
	return  wpjam_yunpian_remote_request($url, compact('mobile','sn'));
}

function wpjam_yunpian_remote_request($url, $data=array()){
	$apikey	= wpjam_get_setting('wpjam-yunpian', 'apikey');

	if(!$apikey){
		return new WP_Error('-1', '未在后台设置 APIKEY！');
	}

	$data['apikey'] = $apikey;

	$response	= wpjam_remote_request(
		$url, 
		array('method' => 'POST', 'body' => $data), 
		array('errcode' => 'code', 'errmsg' => 'msg')
	);

	return $response;
}